#ifndef PIBOT_SIMPLE_DATAFRAME_H_
#define PIBOT_SIMPLE_DATAFRAME_H_

#include "dataframe.h"
#include <string.h>
#include "stdio.h"
static const unsigned short MESSAGE_BUFFER_SIZE = 255;

#define FIX_HEAD 0x5A
#define MES_ID 0x0009
#pragma pack(1)
struct Head{
    unsigned char flag;// 头部标记,固定值:0X5A
    unsigned short int msg_id;// 消息ID,表示消息具体作用,决定消息体具体格式
    unsigned char length;// 消息体长度
};

struct Rdata{
	unsigned short int cmd;
	unsigned char data[MESSAGE_BUFFER_SIZE];
};
#pragma pack()

union
{
    unsigned short data;
    struct
    {
        unsigned char high;
        unsigned char low;
    }bytes;
}u_Smallend2Bigend;

struct Message{
    struct Head head;
	struct Rdata rdata;
    unsigned char check;
    unsigned char recv_count;//已经接收的字节数

    Message(){}
    Message(unsigned short int cmd_id, const unsigned char* data=0,const unsigned char len=0){

        head.flag = FIX_HEAD;
        head.msg_id = MES_ID;
        head.length = recv_count = len + 2;
        u_Smallend2Bigend.bytes.high = (cmd_id >> 8) & 0x00FF;
        u_Smallend2Bigend.bytes.low = cmd_id & 0x00FF;
        rdata.cmd = u_Smallend2Bigend.data;
    
        check = 0;

        if (data != 0 && len !=0)
            memcpy((void*)&this->rdata.data, data, len);
        
        unsigned char* _send_buffer = (unsigned char*)this;

        unsigned int i = 0;
        for (i = 0; i < sizeof(head)+head.length; i++)
            check += _send_buffer[i];
        
        _send_buffer[sizeof(head)+head.length] = check;
    }
};

enum RECEIVE_STATE{
	STATE_RECV_FIX=0,
	STATE_RECV_ID1,
	STATE_RECV_ID2,
	STATE_RECV_LEN,
	STATE_MAIN_CMD,
	STATE_SEED_CMD,
	STATE_RECV_DATA,
	STATE_RECV_CHECK
};


#endif
